package com.gxc.base64;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.springframework.util.Base64Utils;

/**
 * 获取图片的Base64串,再装换成InputStream.
 *
 * @author 宫新程
 * @since 2019/1/29 9:27
 */
public class Base64Test2 {

  private static final String FILE_PATH = "C:\\Users\\Administrator\\Desktop\\test.png";

  /** 服务商会传过来一个base64的串(图片), 需要将图片上传到文件服务器, 返回图片地址 */
  public static void main(String[] args) throws Exception {
//    File picFile = new File(FILE_PATH);
//    String fileType = picFile.getName().substring(picFile.getName().lastIndexOf(".") + 1);
//    System.out.println("File Name is ：" + fileType);
//    System.out.println("file.length：" + picFile.length());
//
//    InputStream inputStream = new FileInputStream(picFile);
//    byte[] bytes = new byte[inputStream.available()];
//    inputStream.read(bytes);
//    System.out.println("bytes.length：" + bytes.length);
//    inputStream.close();
//
//    String base64String = Base64Utils.encodeToString(bytes);
//    System.out.println(base64String);

    String base64String = "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAYAAADtt+XCAAAW3klEQVR42u3d247kqBJA0fr/n+7zdKRRqysTiAgI7LUlSzNdWS4Sm9jc+fn5+fnjcrnmL+Dt/AgELheBAATichEIQCAuF4EABOJyEQhAIC6Xi0AAAnG5CAQgkH8XaEHORSDABoGsBN9uAZtAXAQCEAiBuAgEIBACcREIQCACtstFIACBuFwEAhCIy0UgwDsE8ktCXjc1+KZ8cBEIcEQgC4lKKbzVgWH2XjvS7iIQ4NUCGfn7VUG4Mp3ZcnIRCPA6gVR152T//r/uMZvOUVEQCIEABPLhc7sFUDUekSGQaF65CARoK5CKbqfdEqj83Uj3neBLIACBPEQgGWMwpjoTCEAgBLJ9/MdFIIAxkIKCRyAuAgFeLpAdrZodQptNJ4EQCEAgm9eBEIiLQICXC2RWCgTiIhCAQEqnx54eA3FGiQsgEAIhEBeBAN0EsjKmQSAuAgEIJDz+sUsglYGeQAgEIJANXVi3CuTT5wmEQAACKRbIhwS220zx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QLJnik124q4vZYCAiEQAmkhkE8195sFMnPPyvQCBEIgjxTIt/8e/beTAslcY/KpBWM6L3YJpPo+WeMe3cT1iPdghyRWBZKx71Vkmmy2QGYG8aMyJhAQCIEQyGGBjNbwV9MxM8bz7b6ZXWMAgRDIlEAqNgub7dKZFUREUFkCydqO5dP04N8Kxkh6CQSVAsmaurtTdFmSqZbVVSvRKxJXsRtvRCAZ3UMVLaUqIZMHCIRAtnVh3RJwRlsu0a6nUUlUtKQy0qklAgIhkG0tkJte4NUaekQGK9PwsgXyr6nMlecuACuBeec02AqhnTpi10r0zQKZtXjmg490tUWkTQQgEAIhEAAEQiAEAoBAMrpQTx2Hm/UdOx+lSyAACIRACAQAgRAIgQAgkG3ngVSkofqe1cIhEAAEQiAEAoBACIRAAOC4oCKfr9gosWLMJ5JmAgFAIARCIABAIAQCAMtBtGJsZDZt3eQzm2ZjIAAIhEAIBACBEAiBAMCyQKrvUxFcqzdQfMvRuAQCgEAIhEAAEAiBEAiACwVSPdZRfaZHRGKz+fCETRYJBACBEAiBACAQAiEQAASy5dyP2c/v/I42UwRAIARCIABAIARCIACuE0i3421nJTP73du/B4oCAAIhEAIBQCAEQiAA7hNIhYhmA3MkwFccV1uRHtN4ARAIgRAIAAIhEAIBgNKAlzV9NWt1eIUYZ/MqkmYCAUAgBEIgAAiEQAgEAAACAQAQCACAQAAABAIAIBAAAAgEAFDC/wAlzJkH9h2dxwAAAABJRU5ErkJggg==";

    /** 服务商会传过来一个base64的串(图片), 需要将图片上传到文件服务器, 返回图片地址 */
    byte[] base64Bytes = Base64Utils.decodeFromString(base64String);
    InputStream is = new ByteArrayInputStream(base64Bytes);
    FileOutputStream fos =
        new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\test2.png"));
    int len;
    byte[] buff = new byte[1024];
    while ((len = is.read(buff)) != -1) {
      fos.write(buff);
    }
    fos.close();
    is.close();
  }
}
